import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 路由规则配置
const routes = [
  // 登录
  { path: '/login', component: () => import('../views/login/Login.vue') },
  // 首页
  {
    path: '/home', component: () => import('../views/Home.vue'),
    children: [
      { path: 'users', component: () => import('../views/user/Users.vue') },
      { path: 'roles', component: () => import('../views/role/Roles.vue') },
      { path: 'rights', component: () => import('../views/role/Rights.vue') },
      { path: 'goods', component: () => import('../views/good/Goods.vue') },
      { path: 'params', component: () => import('../views/good/Params.vue') },
      { path: 'categories', component: () => import('../views/good/Categories.vue') }, 
      { path: 'orders', component: () => import('../views/order/Orders.vue') }, 
      { path: 'reports', component: () => import('../views/reports/Reports.vue') }, 
    ]
  }
]

const router = new VueRouter({
  routes
})

// 全局路由前置守卫
router.beforeEach((to, from, next) => {
  // 访问login直接放行
  if (to.path == '/login') {
    next()
  } else {
    // 除了login之外的路由路径访问都要拦截
    if (!sessionStorage.getItem('token')) {
      alert('未登录 请先登录')
      next('/login')
    } else {
      next()
    }
  }
})

export default router
